package com.app;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.bean.ClassmateUserInfoBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ClassmateDetailActivity extends AppCompatActivity {

    private TextView idStr;
    private TextView nameStr;
    private TextView classStr;
    private TextView telephoneStr;
    private TextView emailStr;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_mate_detail);
//    Intent intent = new Intent();不显示的原因
    id =getIntent().getStringExtra("id");
        Toast.makeText(ClassmateDetailActivity.this, id, Toast.LENGTH_SHORT);
    initView();
    initData();
    }
        private void initView(){
            idStr = (TextView) findViewById(R.id.std_info_id_tv);
            nameStr = (TextView) findViewById(R.id.std_info_name_tv);
            classStr = (TextView) findViewById(R.id.std_info_class_tv);
            telephoneStr = (TextView) findViewById(R.id.std_info_telephone_tv);
            emailStr = (TextView) findViewById(R.id.std_info_email_tv);

    }
    private void initData() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(ClassmateDetailActivity.this);
        String url = "http://www.xhjjyst.com/freshmenapp/classmate/findone";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                ClassmateUserInfoBean classmateUserInfoBean = gson.fromJson(jsonObject.toString(), ClassmateUserInfoBean.class);
                idStr.setText(classmateUserInfoBean.getData().getId() + "");
                nameStr.setText(classmateUserInfoBean.getData().getName() + "");
                classStr.setText(classmateUserInfoBean.getData().getClassName() + "");
                telephoneStr.setText(classmateUserInfoBean.getData().getTelephone() + "");
                emailStr.setText(classmateUserInfoBean.getData().getEmail() + "");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ClassmateDetailActivity.this, "网络异常", Toast.LENGTH_SHORT).show();

            }
        });
        requestQueue.add(jsonObjectRequest);
    }
        }

